package lecture18_notepad;

import java.util.Scanner;

public class SimpleNotepad implements INotepad {

	private Page[] pages;
	private int numberOfPages;
	
/*	

TOVA E NOVATA PROMQNAAAAA
EI TYKA ZA PARVI PAT MI STANA LOSHO

new version
*/
	
	SimpleNotepad(int numberOfPages) {
		if (numberOfPages <= 0) {
			System.err.println("Invalid number of pages!");
			return;
		} else {

			this.numberOfPages = numberOfPages;
			pages = new Page[numberOfPages];
			// sega masivat ima elementi, no te sa null
			
			Scanner sc = new Scanner(System.in);
			for(int i=0; i<numberOfPages; i++) {
				System.out.println("Enter title of page " + (i+1) + ": ");
				String title = sc.nextLine();
				pages[i] = new Page(title);
			}
		}
	}
	
	
	
	public void addTextToPage(int page, final String textToAdd) {
		if(page < 1 || page > this.numberOfPages) {
			System.err.println("Invalid page number!");
			return;
		} else {
			this.pages[page-1].addText(textToAdd);
		}
	};
		
	public void replaceText(int page, String newText) {
		if(page < 1 || page > this.numberOfPages) {
			System.err.println("Invalid page number!");
			return;
		} else {
			this.pages[page-1].deleteText();
			this.pages[page-1].addText(newText);
		}
	};
	
	
	public void deleteText(int page) {
		if(page < 1 || page > this.numberOfPages) {
			System.err.println("Invalid page number!");
			return;
		} else {
			this.pages[page-1].deleteText();
		}
	};
	
	// overloads the upper method
	public void deleteText(Page selectedPage) {
		
		// @ TODO:
		// check if selectedPage is an object from the array, but not an outer page
		
		if(selectedPage == null) {
			System.err.println("Invalid page selected!");
			return;
		} else {
			selectedPage.deleteText();
		}
	};
	
	
	public void printAllPages() {
		for(int i=0; i<numberOfPages; i++) {
			System.out.println("Page " + (i+1) + ": \n");
			this.pages[i].viewPage();
			System.out.println();
		}
	};
	
   // overrided comment (server modification)
	
	
	
}
